package jp.dev7.android.util;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.test.AndroidTestCase;

import com.google.common.base.Function;

public class DbsTest extends AndroidTestCase {

    protected void setUp() throws Exception {
        super.setUp();
    }

    public void testBundle() {
        Bundle b = new Bundle();
        byte[] bytes = new byte[100];
        b.putByteArray("name", bytes);

        byte[] bytes2 = b.getByteArray("name");
        assertEquals(100, bytes2.length);
    }

    public void testIte() {
        TestDbHelper helper = new TestDbHelper(mContext);
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cur = db.rawQuery("select id, name from TEST", null);

        int i = 0;
        String[] names =
            {
                "TEMJIN",
                "RAIDEN",
                "VIPER II",
                "BELGDOR",
                "APHARMD",
                "DORKAS",
                "FEI-YEN",
                "BAL-BAS-BOW" };
        for (Virtuaroid vr : Dbs.itr(cur, new Function<Cursor, Virtuaroid>() {
            @Override
            public Virtuaroid apply(Cursor input) {
                final Virtuaroid result = new Virtuaroid();
                result.setId(input.getInt(0));
                result.setName(input.getString(1));
                return result;
            }
        })) {
            assertEquals(i + 1, vr.getId());
            assertEquals(names[i], vr.getName());
            i++;
        }

    }

}

class TestDbHelper extends SQLiteOpenHelper {

    public TestDbHelper(Context context) {
        super(context, "test.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("drop table if exists TEST");
        db.execSQL("create table TEST ("
            + "id integer primary key autoincrement,"
            + "name text"
            + ")");
        db.execSQL("insert into TEST (name) values ('TEMJIN')");
        db.execSQL("insert into TEST (name) values ('RAIDEN')");
        db.execSQL("insert into TEST (name) values ('VIPER II')");
        db.execSQL("insert into TEST (name) values ('BELGDOR')");
        db.execSQL("insert into TEST (name) values ('APHARMD')");
        db.execSQL("insert into TEST (name) values ('DORKAS')");
        db.execSQL("insert into TEST (name) values ('FEI-YEN')");
        db.execSQL("insert into TEST (name) values ('BAL-BAS-BOW')");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int f, int t) {
        onCreate(db);
    }

}

class Virtuaroid {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}